package com.lwbldy.common.tools;

import org.springframework.util.Base64Utils;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Date;
import java.util.UUID;

public class AesUtil {
    private static final String KEY_ALGORITHM = "AES";

    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    /**
     * AES 加密操作
     *
     * @param content  待加密内容
     * @param password 加密密码
     * @return 返回Base64转码后的加密数据
     */
    public static String encrypt(String content, String password) {
        try {
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);

            byte[] byteContent = content.getBytes("UTF-8");

            cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));

            byte[] result = cipher.doFinal(byteContent);

            return Base64Utils.encodeToString(result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    /**
     * AES 解密操作
     *
     * @param content  密文
     * @param password 密钥
     * @return String
     */
    public static String decrypt(String content, String password) {

        try {
            //实例化
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);

            //使用密钥初始化，设置为解密模式
            cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));

            //执行操作
            byte[] result = cipher.doFinal(Base64Utils.decodeFromString(content));
            String s = new String(result, "UTF-8");
            return s;
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    /**
     * 生成加密秘钥
     *
     * @return SecretKeySpec
     */
    private static SecretKeySpec getSecretKey(String password) {
        //返回生成指定算法密钥生成器的 KeyGenerator 对象
        KeyGenerator kg = null;
        try {
            kg = KeyGenerator.getInstance(KEY_ALGORITHM);
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(password.getBytes());
            //AES 要求密钥长度为 128
            kg.init(128, random);
            //生成一个密钥
            SecretKey secretKey = kg.generateKey();
            return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        //签文
        String origin = "abc";

        //加密操作
        String encrypt = AesUtil.encrypt(origin, "3FsEhL44mGVaFtqOVtISQZDoYcSCsRKhcX9ACREPxxD6bXGto4XdarJ4ybbTjFMD");
        //解密操作
        String decrypt = AesUtil.decrypt(encrypt, "3FsEhL44mGVaFtqOVtISQZDoYcSCsRKhcX9ACREPxxD6bXGto4XdarJ4ybbTjFMD");

        System.out.println(encrypt);
        System.out.println(decrypt);

        String requestNo = UUID.randomUUID().toString().replace("-","");
        String requestTime = new Date().getTime()+"";
        String routeInfo =  "yd_receive_event_info";
        String pri = "3FsEhL44mGVaFtqOVtISQZDoYcSCsRKhcX9ACREPxxD6bXGto4XdarJ4ybbTjFMD";
        String sing = AesUtil.encrypt(requestNo+requestTime+routeInfo,pri);

        String jsonStr = "{\n" +
                "\t\"data\": {\n" +
                "\t\t\"id\": \"3\",\n" + //
                "\t\t\"eventNumber\": \"123\",\n" + //
                "\t\t\"title\": \"2\",\n" + //
                "\t\t\"content\": \"1\",\n" + //
                "\t\t\"eventStatus\": \"1\",\n" +
                "\t\t\"eventTime\": \"2022-9-16 14:29:13\",\n" +
                "\t\t\"location\": \"浙江省杭州市西湖区翠苑街道富丽科技大厦A座华星发展大厦A座\",\n" +
                "\t\t\"latitude\": \"30.282876\",\n" +
                "\t\t\"longitude\": \"120.124982\",\n" +
                "\t\t\"eventLevel\": \"普通\",\n" +
                "\t\t\"isMajor\": \"1\",\n" +
                "\t\t\"reportName\": \"cyh\",\n" +
                "\t\t\"reportDepartmentName\": \"于都\",\n" +
                "\t\t\"imageUrls\": \"\",\n" +
                "\t\t\"fileUrls\": \"\",\n" +
                "\t\t\"eventType\":\"12\",\n" +
                "\t\t\"eventSource\":\"\",\n" +
                "\t\t\"eventAddress\":\"\",\n" +
                "\t\t\"telPhone\":\"1111111111111\",\n" +
                "\t\t\"time\":\"2023-08-08 11:11:11\",\n" +
                "\t\t\"degreeFlag\":\"1\",\n" +
                "\t\t\"fujian\":\"\"\n" +
                "\t},\n" +
                "\t\"requestNo\": \""+requestNo+"\",\n" +
                "\t\"requestTime\": \""+requestTime+"\",\n" +
                "\t\"routeInfo\": \""+routeInfo+"\",\n" +
                "\t\"sign\": \""+sing+"\"\n" +
                "}";

//        String jsonStr = "{\n" +
//                "\t\"data\": {\n" +
//                "\t\t\"degreeFlag\": \"0\",\n" +
//                "\t\t\"reportName\": \"刘卫国\",\n" +
//                "\t\t\"eventNumber\": \"20231004103904000001\",\n" + //
//                "\t\t\"latitude\": \"25.952069\",\n" +
//                "\t\t\"eventSource\": \"群众反馈\",\n" +
//                "\t\t\"title\": \"推送测试\",\n" +
//                "\t\t\"content\": \"推送测试\",\n" +
//                "\t\t\"telPhone\": \"15078283764\",\n" +
//                "\t\t\"eventStatus\": \"1\",\n" +
//                "\t\t\"reportDepartmentName\": \"于都\",\n" +
//                "\t\t\"id\": \"1709397699188527105\",\n" +
//                "\t\t\"longitude\": \"115.415508\"\n" +
//                "\t},\n" +
//                "\t\"sign\": \"o0mp8+pZbzwgutTGEoPGDX12KZfF0GOh5kdinJgwonLs6Qld40bssRw+n7vtoOgWD3dKhlkR2k8f4Pg1CYfkSqTqQ4CbG/98RKd3cGrhYb0=\",\n" +
//                "\t\"requestNo\": \"SHZQjv34zc0ak6rgezekyn7mj5082472\",\n" +
//                "\t\"routeInfo\": \"yd_receive_event_info\",\n" +
//                "\t\"requestTime\": \"1696387170668\"\n" +
//                "}";

        System.out.println(jsonStr);
        String url = "http://117.163.32.87:446/data/dflow/entrance/json/api";
        String result = HttpRequest.sendJSONPost(jsonStr,url);
        System.out.println(result);
    }
}
